package com.lijin.study.java.thread.pool3;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;


public class ExecutorTest {

	public static void main(String[] args) {
		ExecutorService pool = Executors.newFixedThreadPool(2);
		List<Future<String>> flst = new ArrayList<Future<String>>();
		for (int i = 0; i < 5; i ++) {
			MyTask t = new MyTask("T-" + i, i + 5);
			Future<String> f = pool.submit(t, t.getNm() + " is Done");
			flst.add(f);
		}
		for (int i = 0; i < flst.size(); i ++) {
			try {
				System.out.println(flst);
				System.out.println(flst.get(i).get());
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ExecutionException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		pool.shutdownNow();
		System.out.println(pool);
	}
	
}

class MyTask implements Runnable {
	
	private String nm = "";
	private int cnt = 0;
	public MyTask(String nm, int cnt){
		this.nm = nm;
		this.cnt = cnt;
	}
	public String getNm() {
		return nm;
	}
	@Override
	public void run() {
		for (int i = 1; i < cnt; i ++) {
			System.out.println(nm + " is running" + i);
		}
	}
	
}